<?php

namespace app\admin\model\xapply;

use think\Model;


class App extends Model
{

    

    

    // 表名
    protected $name = 'xapply_app';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'is_use_text',
        'is_notice_text',
        'is_admin_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getIsUseList()
    {
        return ['0' => __('Is_use 0'), '1' => __('Is_use 1')];
    }

    public function getIsNoticeList()
    {
        return ['0' => __('Is_notice 0'), '1' => __('Is_notice 1')];
    }

    public function getIsAdminList()
    {
        return ['0' => __('Is_admin 0'), '1' => __('Is_admin 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsUseTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_use']) ? $data['is_use'] : '');
        $list = $this->getIsUseList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsNoticeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_notice']) ? $data['is_notice'] : '');
        $list = $this->getIsNoticeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsAdminTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_admin']) ? $data['is_admin'] : '');
        $list = $this->getIsAdminList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function category()
    {
        return $this->belongsTo('\app\admin\model\xapply\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function rules()
    {
        return $this->hasMany('\app\admin\model\xapply\Rule', 'app_id', 'id');
    }

    public function messages()
    {
        return $this->hasMany('\app\admin\model\xapply\Message', 'app_id', 'id');
    }
}
